﻿using AutoUpdater.Bll;
using AutoUpdater.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoUpdateSendFileClient
{
    public partial class Form1 : Form
    {
        private string filePath = string.Empty;
        private List<string> FileNameList = new List<string>();

        private SysBasicInfo _sysBasicInfo;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSeletPath_Click(object sender, EventArgs e)
        {
            filePath = string.Empty;
            textBox1.Text = string.Empty;
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            DialogResult result = dialog.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            filePath = dialog.SelectedPath.Trim();
            DirectoryInfo theFolder = new DirectoryInfo(filePath);
            if (!theFolder.Exists)
            {
                MessageBox.Show("目录不存在");
                return;
            }

            richTextBox1.AppendText("已选择文件目录"+ filePath);
        }

        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();//打开文件对话框
            fileDialog.Multiselect = true;//可以选择多个选项
            //fileDialog.InitialDirectory = InitialDirectory;//初始化路径
            //fileDialog.Filter = "txt files (*.txt,*.*)|*.txt;*.*";//过滤选项设置，文本文件，所有文件。
            //fileDialog.FilterIndex = 1;//当前使用第二个过滤字符串
            //fileDialog.RestoreDirectory = true;//对话框关闭时恢复原目录
            fileDialog.Title = "请选择文件";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                FileNameList = fileDialog.FileNames.ToList();
            }
            else
            {
                FileNameList = new List<string>();
            }
        }

        private void btnUplaod_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(filePath) && (FileNameList ==null || FileNameList.Count ==0 ))
            {
                MessageBox.Show("请选择文件");
            }

            if(!string.IsNullOrEmpty(filePath))
            {
                string sendResult = sendDictry(filePath, filePath);
                if(!string.IsNullOrEmpty(sendResult))
                {
                    MessageBox.Show(sendResult);
                    return ;
                }
            }

            if(FileNameList !=null && FileNameList.Count> 0)
            {
                foreach(string file in FileNameList)
                {
                    string sendResult = sendFile(null, filePath);

                    if (!string.IsNullOrEmpty(sendResult))
                    {
                        MessageBox.Show(sendResult);
                        return;
                    }
                }
                
            }

            MessageBox.Show("上传陈功");
        }

        private string sendDictry(string basePath,string path)
        {
            var files = Directory.GetFiles(path);

            foreach(var filePath in files)
            {
                string sendResult = sendFile(basePath,filePath);

                if(!string.IsNullOrEmpty(sendResult))
                {
                    return sendResult;
                }
            }

            var direList = Directory.GetDirectories(path);

            foreach(string paht in direList)
            {
                string sendResult = sendDictry(basePath, paht);

                if (!string.IsNullOrEmpty(sendResult))
                {
                    return sendResult;
                }
            }

            return null;
        }

        private string sendFile(string basePath,string fileName)
        {
            FileInfo fileInfo = new FileInfo(fileName);

            string filePath = "";
            if (!string.IsNullOrEmpty(basePath))
            {
                string fileDirectoryName = fileInfo.DirectoryName;
                filePath = fileDirectoryName.Replace(basePath, "");
                if (filePath.StartsWith("/"))
                {
                    filePath = filePath.Substring(1);
                }
                if (filePath.StartsWith("\\"))
                {
                    filePath = filePath.Substring(1);
                }
            }
            if(!string.IsNullOrEmpty(filePath))
            {
                filePath += "\\";
            }
            filePath += fileInfo.Name;

            Dictionary<string, string> otherData = new Dictionary<string, string>();
            otherData.Add("sysCode", textBox3.Text.Trim());
            otherData.Add("sysVersion", textBox5.Text.Trim());
            otherData.Add("filePath", filePath);
            otherData.Add("fileType", textBox2.Text.Trim());

            byte[] file1 = File.ReadAllBytes(fileName);

            var sendReuslt = FileCheckBll.SendFile(otherData, file1, fileInfo.Name);
            if(sendReuslt.code != 200)
            {
                return sendReuslt.message;
            }
            return null;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //var result = FileCheckBll.CheckBaseSys("360");
            //if(result.code != 200)
            //{
            //    _sysBasicInfo
            //}

        }
    }
}
